//
//  HMHomeViewController.m
//  黑马微博
//
//  Created by apple on 14-7-3.
//  Copyright (c) 2014年 heima. All rights reserved.
//

#import "HMHomeViewController.h"
#import "HMOneViewController.h"
#import "HMTitleButton.h"
#import "HMPopMenu.h"

@interface HMHomeViewController () <HMPopMenuDelegate>
@end

@implementation HMHomeViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 设置导航栏按钮
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImageName:@"navigationbar_friendsearch" highImageName:@"navigationbar_friendsearch_highlighted" target:self action:@selector(friendSearch)];
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImageName:@"navigationbar_pop" highImageName:@"navigationbar_pop_highlighted" target:self action:@selector(pop)];
    
    // 设置导航栏中间的标题按钮
    HMTitleButton *titleButton = [[HMTitleButton alloc] init];
    // 设置文字
    [titleButton setTitle:@"首页" forState:UIControlStateNormal];
    // 设置图标
    [titleButton setImage:[UIImage imageWithName:@"navigationbar_arrow_down"] forState:UIControlStateNormal];
    // 设置背景
    [titleButton setBackgroundImage:[UIImage resizedImage:@"navigationbar_filter_background_highlighted"] forState:UIControlStateHighlighted];
    // 设置尺寸
    titleButton.width = 100;
    titleButton.height = 35;
    // 监听按钮点击
    [titleButton addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.titleView = titleButton;
}

/**
 *  点击标题点击
 */
- (void)titleClick:(UIButton *)titleButton
{
    // 换成箭头向上
    [titleButton setImage:[UIImage imageWithName:@"navigationbar_arrow_up"] forState:UIControlStateNormal];
    
    // 弹出菜单
    UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd];
    button.backgroundColor = [UIColor blueColor];
    
    HMPopMenu *menu = [[HMPopMenu alloc ] initWithContentView:nil];
    menu.delegate = self;
    menu.arrowPosition = HMPopMenuArrowPositionCenter;
//    menu.dimBackground = YES;
    [menu showInRect:CGRectMake(100, 0, 200, 480)];
}

#pragma mark - 弹出菜单协议
- (void)popMenuDidDismissed:(HMPopMenu *)popMenu
{
    HMTitleButton *titleButton = (HMTitleButton *)self.navigationItem.titleView;
    [titleButton setImage:[UIImage imageWithName:@"navigationbar_arrow_down"] forState:UIControlStateNormal];
}

/**
 *  点击了遮盖
 */
- (void)coverClick
{
    HMLog(@"coverClick---");
}

- (void)friendSearch
{
    HMLog(@"friendSearch---");
    
    HMOneViewController *one = [[HMOneViewController alloc] init];
    one.title = @"OneVc";
    [self.navigationController pushViewController:one animated:YES];
}

- (void)pop
{
    HMLog(@"pop---");
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    cell.backgroundColor = [UIColor redColor];
    cell.textLabel.text = [NSString stringWithFormat:@"首页测试数据----%d", indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIViewController *newVc = [[UIViewController alloc] init];
    newVc.view.backgroundColor = [UIColor redColor];
    newVc.title = @"新控制器";
    [self.navigationController pushViewController:newVc animated:YES];
}
@end
